﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChinessChess
{
    class CXe:CQuanCo
    {
        private int new_Index, old_Index;
        
        public CXe() { 

        }
        public CXe(COCo[] chess, int newIndex, int oldIndex, ChessColor color)
            : base(chess, color)
        {
            this.new_Index = newIndex;
            this.old_Index = oldIndex;
        }
        public override bool CheckIndex(){
            if (flag[new_Index].Dong == flag[old_Index].Dong){//neu xe chay hang ngang thi dong giu nguyen cot thay doi
                int index = old_Index;
                int cout = 0;
                if (new_Index < old_Index){
                    while (index > new_Index){
                        if (flag[index].State == true){
                            cout++;
                        }
                        index -= 1;
                    }
                }
                else {
                    while (index < new_Index){
                        if (flag[index].State == true){
                            cout++;
                        }
                        index += 1;
                    }
                }
                if (cout == 1) {
                    return true;
                }
            }
            else {
                int index = old_Index;
                int cout = 0;
                if (new_Index < old_Index){
                    while (index > new_Index){
                        if (flag[index].State == true){
                            cout++;
                        }
                        index -= 9;
                    }
                }
                else{
                    while (index < new_Index){
                        if (flag[index].State == true){
                            cout++;
                        }
                        index += 9;
                    }
                }
                if (cout == 1){
                    return true;
                }
            }
            return false;
        }
        public override bool CheckPoint(){
            if (flag[new_Index].Dong == flag[old_Index].Dong || flag[new_Index].Cot == flag[old_Index].Cot)
                return true;
            return false;
        }
        public override bool KiemTraChieuTuong(int gIndex, int qIndex)
        {
            Boolean isCheck = false;
            if (flag[gIndex].Dong == flag[qIndex].Dong || flag[gIndex].Cot == flag[qIndex].Cot){
                isCheck = true;
            }
            if (isCheck) {
                if (flag[gIndex].Dong == flag[qIndex].Dong)
                {
                    int index = qIndex;//vi tri cua quan tuong
                    int cout = 0;
                    if (gIndex > qIndex)
                    {
                        while (index > gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index++;
                        }
                    }
                    else {
                        while (index < gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index--;
                        }
                    }
                    if (cout == 1) {
                        return true;
                    }
                }
                else {
                    int index = qIndex;//vi tri cua quan tuong
                    int cout = 0;
                    if (gIndex > qIndex)
                    {
                        while (index > gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index+=9;
                        }
                    }
                    else
                    {
                        while (index < gIndex)
                        {
                            if (flag[index].State == true)
                            {
                                cout++;
                            }
                            index-=9;
                        }
                    }
                    if (cout == 1)
                    {
                        return true;
                    }
                }
            }
            return isCheck;
        }
    }
}
